<?php
/**
 * 通过unionId获取推广链接【申请】 jd.union.open.promotion.byunionid.get
 *
 * See: https://union.jd.com/openplatform/api/631
 * User: Ken.Zhang <kenphp@yeah.net>
 * Date: 2019-05-12
 */
class UnionOpenPromotionByunionidGetRequest {

    /**
     * 推广物料链接，建议链接使用微Q前缀，能较好适配微信手Q页面
     */
    private $materialId;

    /**
     * 目标推客的联盟ID
     */
    private $unionId;

    /**
     * 新增推广位id （不填的话，为其默认生成一个唯一此接口推广位-名称：微信手Q短链接）
     */
    private $positionId;

    /**
     * 子帐号身份标识，格式为子站长ID_子站长网站ID_子站长推广位ID
     */
    private $pid;

    /**
     * 优惠券领取链接，在使用优惠券、商品二合一功能时入参，且materialId须为商品详情页链接
     */
    private $couponUrl;

    /**
     * 子联盟ID（需要联系运营开通权限才能拿到数据）
     */
    private $subUnionId;

    /**
     * 请求参数
     */
    public $apiParams = [];

    public function setMaterialId($material_id)
    {
        $this->materialId = $material_id;
        $this->apiParams['promotionCodeReq']['materialId'] = $material_id;
    }

    public function getMaterialId()
    {
        return $this->materialId;
    }

    public function setUnionId($union_id)
    {
        $this->unionId = $union_id;
        $this->apiParams['promotionCodeReq']['unionId'] = $union_id;
    }

    public function getUnionId()
    {
        return $this->unionId;
    }

    public function setPositionId($position_id)
    {
        $this->positionId = $position_id;
        $this->apiParams['promotionCodeReq']['positionId'] = $position_id;
    }

    public function getPositionId()
    {
        return $this->positionId;
    }

    public function setPid($pid)
    {
        $this->pid = $pid;
        $this->apiParams['promotionCodeReq']['pid'] = $pid;
    }

    public function getPid()
    {
        return $this->pid;
    }

    public function setCouponUrl($coupon_url)
    {
        $this->couponUrl = $coupon_url;
        $this->apiParams['promotionCodeReq']['couponUrl'] = $coupon_url;
    }

    public function getCouponUrl()
    {
        return $this->couponUrl;
    }

    public function setSubUnionId($sub_union_id)
    {
        $this->subUnionId = $sub_union_id;
        $this->apiParams['promotionCodeReq']['subUnionId'] = $sub_union_id;
    }

    public function getSubUnionId()
    {
        return $this->subUnionId;
    }

    public function getApiMethodName()
    {
        return 'jd.union.open.promotion.byunionid.get';
    }

    /**
     * 解析结果
     *
     * @param   string      $response
     * @throws  Exception
     * @return  mixed
     */
    public function getResult($response)
    {
        $responseKey = str_replace('.', '_', $this->getApiMethodName()) . '_response';

        $response = @json_decode($response, true);
        if (!array_key_exists($responseKey, $response))
            $resp = $response['errorResponse'];
        else {
            $response[$responseKey]['result'] = json_decode($response[$responseKey]['result']);

            $resp = $response[$responseKey];
        }

        return $resp;
    }

}